//
//  MLXBankSacnViewController.m
//  Pods
//
//  Created by SY on 2019/1/28.
//

#import "MLXBankSacnViewController.h"
#import "MLXScanBankCardView.h"
#import "UIImage+Stream.h"
@interface MLXBankSacnViewController ()
@property (nonatomic,strong)MLXScanBankCardView * scanBankCardView;
@end

@implementation MLXBankSacnViewController

#pragma mark - 检测是模拟器还是真机
#if TARGET_IPHONE_SIMULATOR
// 是模拟器的话，提示“请使用真机测试！！！”
-(void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = @"银行卡扫描";
    UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"模拟器没有摄像设备"  message:@"请使用真机测试！！！" preferredStyle:UIAlertControllerStyleActionSheet];
    [alert addAction:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDestructive handler:^(UIAlertAction *action) {
        [self.navigationController popViewControllerAnimated:true];
    }]];
    [self presentViewController:alert animated:YES completion:nil];
}

#else
#ifdef exbankcard
#pragma mark - 懒加载
#pragma mark device
-(void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    [self.navigationController.navigationBar setHidden:NO];
}
-(MLXScanBankCardView *)scanBankCardView{
    if (!_scanBankCardView) {
        _scanBankCardView = [[MLXScanBankCardView alloc]initWithFrame:[UIScreen mainScreen].bounds];
    }
    return _scanBankCardView;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self.navigationController.navigationBar setHidden:YES];
    // Do any additional setup after loading the view.
    self.title = @"银行卡扫描";
    [self.view insertSubview:self.scanBankCardView atIndex:0];
    self.scanManager.sessionPreset = AVCaptureSessionPresetHigh;
    if ([self.scanManager configBankScanManager]) {
        UIView *view = [[UIView alloc] initWithFrame:self.view.bounds];
        [self.view insertSubview:view atIndex:0];
        AVCaptureVideoPreviewLayer * previewLayer = [[AVCaptureVideoPreviewLayer alloc]initWithSession:self.scanManager.captureSession];
        previewLayer.frame = [UIScreen mainScreen].bounds;
        previewLayer.videoGravity = AVLayerVideoGravityResizeAspectFill;
        [view.layer addSublayer:previewLayer];
        [self.scanManager startSession];
    }
    __weak typeof (self) weakSelf = self;
    self.scanManager.bankCardesult = ^(id result) {
        MLXBankCardModel * model = (MLXBankCardModel *)result;
        [weakSelf showResult:model];
    };
}

- (void)showResult:(MLXBankCardModel *)result {
   !self.identifyCompletionBlock ?: self.identifyCompletionBlock(result);
}
#endif
#endif
@end
